from threading import Timer

from splinter.browser import Browser
from  time import  sleep

from  SinaWeibo import  Weibo

lst=[]
class AutoSend(object):
  # 根据验证框的存在与否判断是否要输入验证码
  def isVerifyCodeExist(self):
        try:  # 如果成功找到验证码输入框返回true
            self.driver.driver.find_element_by_css_selector('input[name="verifycode"]')
            return True
        except:  # 如果异常返回false
            return False


  def loadBasicInfo(self):
    # 登录的url
     self.loginUrl = 'https://www.weibo.com/'
     self.driver = Browser(driver_name='chrome',executable_path='chromedriver')

  """自动发送"""
  def autoSend(self):
      self.driver.driver.find_element_by_class_name('W_btn_a').click()
      """上传图片"""
  def upload_pic(self,text, *path):
     #  input_p = self.driver.driver.find_element_by_xpath('//div[@class="kind"]/a[@title="图片"]')
     #  input_p.click()
     #  #填写数据
     # # input_w.send_keys(path)
     wb = Weibo(lst[0], lst[1])
     wb.postImage(text, *path)
  def upload_text(self,text):
      #用xpath语法找到输入框
      input_w=self.driver.driver.find_element_by_xpath('//div[@node-type="textElDiv"]/textarea[@class="W_input"]')
      input_w.send_keys(text)
  def login(self):
        print('开始登录..')
        f = open("info.txt")
        global lst
        for line in f:
            lst.append(line)
            # lst=self.driver.readInfo()
        print(len(lst))
        # 登录
        self.driver.visit(self.loginUrl)
        print(lst[0])
        self.username = lst[0]  # input("\n请输入用户名，输入按回车。。。")
        # 合法性判断
        while True:
            if self.username == '':
                self.username = input("\n请输入用户名，输入按回车。。。")
            else:
                break
        self.password = lst[1]  # input("\n请输入密码，输入按回车")
        print(lst[1])
        # 合法性判断
        while True:
            if self.password == '':
                self.password = input("\n请输入密码，输入按回车")
            else:
                break
        sleep(10)
        #找到元素后自动填充账号密码
        input_account = self.driver.driver.find_element_by_id('loginname')  # 找到账号输入框
        input_psw = self.driver.driver.find_element_by_css_selector('input[type="password"]')  # 找到密码输入框

        input_account.send_keys(self.username)
        input_psw.send_keys(self.password)

        bt_logoin = self.driver.driver.find_element_by_class_name('login_btn')  # 找到登录按钮
        print(self.isVerifyCodeExist())
        sleep(3)
        while self.isVerifyCodeExist():
            input_verifycode = self.driver.driver.find_element_by_css_selector('input[name="verifycode"]')  # 验证码输入框
            bt_logoin.click()
            # print('输入验证码，（输入“c”切换验证码图片）')
            # verifycode=input('\n输入验证码')
            # if verifycode=='c':
            #     bt_logoin.click()
            # else:
            #     input_verifycode.send_keys(verifycode)
            #     bt_logoin.click()
                # 点击完登录以后判断是否成功
            if  len(self.driver.url.split('/'))>0:  # 如果连接已经跳转到home
                 print(u'登录成功')
                 break
            # else:
            #      print(u'输入的验证码不正确')


        # bt_logoin.click()  # 点击登录
  def autoSendByTimer(self):
      while True:
          #获取发送的内容
          # 发送文本微博
          autosend.upload_text("python自动发送")
          autosend.autoSend()
          sleep(180) #3分钟执行一次


        # print(u'登录成功')
if __name__=='__main__':
    autosend = AutoSend()
    autosend.loadBasicInfo()
    autosend.login()
    sleep(10)
    t=Timer(3,autosend.autoSendByTimer)
    t.start()

    print("okkkk")
    # 发送文本微博
    autosend.upload_text("python自动发送")
    autosend.autoSend()
   # 发送图片微博
    autosend.upload_pic("python自动发送","2.png","2.png")